//
//  SISSContractTaxChangeOfPolicyCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/10/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractTaxChangeOfPolicyCell.h"

@interface SISSContractTaxChangeOfPolicyCell ()<UITextFieldDelegate>

/// checkOneButton
@property (weak, nonatomic) UIButton *checkOneButton;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkTwoButton;
/// checkThreeButton
@property (weak, nonatomic) UIButton *checkThreeButton;

/// oneLabel
@property (weak, nonatomic) YYLabel *oneLabel;
/// twoLabel
@property (weak, nonatomic) YYLabel *twoLabel;

/// textField
@property (weak, nonatomic) UITextField *textField;

/// buyerLabel
@property (weak, nonatomic) UILabel *buyerLabel;
/// buyerTextField
@property (weak, nonatomic) UITextField *buyerTextField;
/// buyerUnitLabel
@property (weak, nonatomic) UILabel *buyerUnitLabel;
/// sellerLabel
@property (weak, nonatomic) UILabel *sellerLabel;
/// sellerTextField
@property (weak, nonatomic) UITextField *sellerTextField;
/// sellerUnitLabel
@property (weak, nonatomic) UILabel *sellerUnitLabel;

/* oneLine */
@property (weak, nonatomic) UIView *oneLine;
/* twoLine */
@property (weak, nonatomic) UIView *twoLine;

/// checkedButton
@property (weak, nonatomic) UIButton *checkedButton;


@end
@implementation SISSContractTaxChangeOfPolicyCell


- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    if (contractModel.firstIndex == 1) {
        self.checkedButton = self.checkOneButton;
    } else if (contractModel.firstIndex == 2) {
        self.checkedButton = self.checkTwoButton;
    } else if (contractModel.firstIndex == 3) {
        self.checkedButton = self.checkThreeButton;
    }
    self.checkedButton.selected = self.checkedButton ? YES : NO;
    
    self.textField.userInteractionEnabled = contractModel.firstIndex == 2;
    self.buyerTextField.userInteractionEnabled = contractModel.firstIndex == 3;
    self.sellerTextField.userInteractionEnabled = contractModel.firstIndex == 3;
    self.textField.text = contractModel.firstIndex == 2 ? contractModel.rightStr : @"";
    self.buyerTextField.text = contractModel.firstIndex == 3 ? contractModel.rightStr : @"";
    self.sellerTextField.text = contractModel.rightTwoStr;
}


#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    self.checkedButton.selected = NO;
    self.checkedButton = button;
    self.checkedButton.selected = !button.selected;
    self.textField.userInteractionEnabled = button.tag == 101;
    self.buyerTextField.userInteractionEnabled = button.tag == 102;
    self.sellerTextField.userInteractionEnabled = button.tag == 102;
    if (button.tag != 101)  self.textField.text = @"";
    if (button.tag != 102) {
        self.sellerTextField.text = @"";
        self.buyerTextField.text = @"";
    }
    self.contractModel.firstIndex = button.tag - 99;
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if (textField == self.textField) {
        self.contractModel.rightStr = textField.text;
    } else if (textField == self.buyerTextField) {
        self.contractModel.rightStr = textField.text;
    } else if (textField == self.sellerTextField) {
        self.contractModel.rightTwoStr = textField.text;
    }
}




#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [self creatButtonWithTag:100];
    [self.contentView addSubview:button];
    self.checkOneButton = button;
    
    YYLabel *oneLabel = [[YYLabel alloc] init];
    oneLabel.numberOfLines = 0;
    oneLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    oneLabel.textColor = SECONDARY_TEXT_COLOR;
    oneLabel.font = [UIFont systemFontOfSize:16];
    oneLabel.text = @"由法律、法规和、政策和政府规定的缴纳方缴纳。";
    [self.contentView addSubview:oneLabel];
    self.oneLabel = oneLabel;
    
    UIView *oneLine = [[UIView alloc] init];
    oneLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:oneLine];
    self.oneLine = oneLine;
    
    UIButton *checkTwoButton = [self creatButtonWithTag:101];
    [self.contentView addSubview:checkTwoButton];
    self.checkTwoButton = checkTwoButton;
    
    
    
    YYLabel *twoLabel = [[YYLabel alloc] init];
    twoLabel.textColor = SECONDARY_TEXT_COLOR;
    NSString *str = @"买卖双方同意由 请选择买卖双方 方缴纳和承担一切责任，非缴纳税费方应配合缴纳方按政府规定提交相应税费优惠减免资料，不符合税费优惠减免条件的除外。";
    NSMutableAttributedString *twoAttributedText = [[NSMutableAttributedString alloc] initWithString:str attributes:nil];
    [twoAttributedText yy_setTextHighlightRange:NSMakeRange(6, 8) color:[UIColor darkGrayColor] backgroundColor:[UIColor groupTableViewBackgroundColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        UIResponder *responder = self;
        UIViewController *viewController;
        while ((responder = [responder nextResponder]))
            if ([responder isKindOfClass: [UIViewController class]])
                viewController = (UIViewController *)responder;
//        viewController 
    }];
    twoLabel.attributedText = twoAttributedText;
    twoLabel.font = [UIFont systemFontOfSize:16];
    twoLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    twoLabel.numberOfLines = 0;
    twoLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:twoLabel];
    self.twoLabel = twoLabel;
    
    UIView *twoLine = [[UIView alloc] init];
    twoLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:twoLine];
    self.twoLine = twoLine;
    
    UIButton *checkThreeButton = [self creatButtonWithTag:102];
    [self.contentView addSubview:checkThreeButton];
    self.checkThreeButton = checkThreeButton;
    
    UILabel *buyerLabel = [[UILabel alloc] init];
    buyerLabel.text = @"买方支付：";
    buyerLabel.textColor = MAIN_TEXT_COLOR;
    [self.contentView addSubview:buyerLabel];
    self.buyerLabel = buyerLabel;
    
    UITextField *buyerTextField = [[UITextField alloc] init];
    buyerTextField.delegate = self;
    buyerTextField.textColor = MAIN_TEXT_COLOR;
    buyerTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    buyerTextField.keyboardType = UIKeyboardTypeDecimalPad;
    buyerTextField.placeholder = @"几";
    buyerTextField.userInteractionEnabled = NO;
    buyerTextField.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:buyerTextField];
    self.buyerTextField = buyerTextField;
    
    UILabel *buyerUnitLabel = [[UILabel alloc] init];
    buyerUnitLabel.text = @"%";
    buyerUnitLabel.textColor = MAIN_TEXT_COLOR;
    [self.contentView addSubview:buyerUnitLabel];
    self.buyerUnitLabel = buyerUnitLabel;
    
    UILabel *sellerLabel = [[UILabel alloc] init];
    sellerLabel.text = @"卖方支付：";
    sellerLabel.textColor = MAIN_TEXT_COLOR;
    [self.contentView addSubview:sellerLabel];
    self.sellerLabel = sellerLabel;
    
    UITextField *sellerTextField = [[UITextField alloc] init];
    sellerTextField.delegate = self;
    sellerTextField.textColor = MAIN_TEXT_COLOR;
    sellerTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    sellerTextField.keyboardType = UIKeyboardTypeDecimalPad;
    sellerTextField.placeholder = @"几";
    sellerTextField.textAlignment = NSTextAlignmentRight;
    sellerTextField.userInteractionEnabled = NO;
    [self.contentView addSubview:sellerTextField];
    self.sellerTextField = sellerTextField;
    
    UILabel *sellerUnitLabel = [[UILabel alloc] init];
    sellerUnitLabel.text = @"%";
    sellerUnitLabel.textColor = MAIN_TEXT_COLOR;
    [self.contentView addSubview:sellerUnitLabel];
    self.sellerUnitLabel = sellerUnitLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.contentView).offset(5 * margin);
    }];
    
    [self.checkOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.centerY.equalTo(weakSelf.oneLabel);
    }];
    [self.oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
    }];
    [self.checkTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.twoLabel);
    }];
    
    [self.buyerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.twoLabel).offset(2 * margin);
        make.top.equalTo(weakSelf.twoLabel.mas_bottom).offset(2 * margin);
    }];
    [self.buyerTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.buyerLabel.mas_right).offset(2 * margin);
        make.centerY.equalTo(weakSelf.buyerLabel);
        make.right.equalTo(weakSelf.buyerUnitLabel.mas_left).offset(-2 * margin);
        make.width.mas_equalTo(8 * margin);
    }];
    [self.buyerUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.buyerLabel);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.sellerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.twoLabel).offset(2 * margin);
        make.top.equalTo(weakSelf.buyerLabel.mas_bottom).offset(2 * margin);
    }];
    [self.sellerTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.sellerLabel.mas_right).offset(2 * margin);
        make.centerY.equalTo(weakSelf.sellerLabel);
        make.width.mas_equalTo(8 * margin);
        make.right.equalTo(weakSelf.sellerUnitLabel.mas_left).offset(-2 * margin);
    }];
    [self.sellerUnitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.sellerLabel);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.checkThreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.buyerLabel.mas_bottom).offset(margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.sellerLabel.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
